package com.ruoyi.marketing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.marketing.domain.Coupon;
import com.ruoyi.marketing.mapper.CouponMapper;
import com.ruoyi.marketing.service.CouponService;
import com.ruoyi.marketing.service.impl.CouponServiceImpl;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.ruoyi.common.core.utils.PageUtils.startPage;

@RestController
public class CouponController extends BaseController {


    @Resource
    private CouponService couponService;

    @Resource
    private CouponMapper couponMapper;

    //返回优惠券信息
    @GetMapping("/getCoupon")
    public TableDataInfo getCouponList(@RequestParam(required = false, defaultValue = "") String couponName,
                                       @RequestParam(required = false, defaultValue = "") String couponIsOpen,
                                       @RequestParam Integer pageNum,
                                       @RequestParam Integer pageSize) {
        //模糊查询

        QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
        wrapper.like("name", couponName)
                .like("coupon_is_open", couponIsOpen);
        //分页
        PageHelper.startPage(pageNum, pageSize);
        List<Coupon> list = couponMapper.selectList(wrapper);
        return getDataTable(list);


    }

    //删除单个优惠券
    @DeleteMapping("/deleteCoupon/{couponId}")
    public AjaxResult deleteCoupon(@PathVariable Integer couponId) {
        int i = couponMapper.deleteById(couponId);
        if (i == 1) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败");
        }
    }


    /*批量删除*/
    @DeleteMapping("/deleteCoupon")
    public AjaxResult deleteCouponBatchIds(@RequestBody List<Integer> couponIds) {
        int i = couponMapper.deleteBatchIds(couponIds);
        if (i >= 1) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败");
        }
    }


    /*修改、添加优惠券信息*/
    @PostMapping("/saveOrUpdateCoupon")
    public AjaxResult saveOrUpdateCoupon(@RequestBody Coupon coupon) {
        if (couponService.saveOrUpdate(coupon)) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.error("操作失败");
        }
    }

    @PostMapping("saveCoupon")
    public AjaxResult saveCoupon(@RequestBody Coupon coupon){
        System.out.println(coupon.toString());
        Boolean i = couponService.save(coupon);
        return i ? AjaxResult.success("保存成功!") : AjaxResult.error("保存失败！");
    }

    //根据id查优惠券信息
    @GetMapping("/getCouponById")
    public Coupon getCouponById(@RequestParam Integer couponId) {

        return couponMapper.selectById(couponId);
    }

    @PostMapping("/copyCoupon")
    public AjaxResult copyCoupon(@RequestBody Integer id){
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
        wrapper.eq("couponId",id);
        Coupon coupon = couponService.getOne(wrapper);
        Boolean i = couponService.save(coupon);
        return i ? AjaxResult.success("复制成功！") : AjaxResult.error("复制失败！");
    }

}
